package com.android.myapplication.view.database;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.myapplication.R;

import androidx.cursoradapter.widget.CursorAdapter;

public class MyCursorAdapter extends CursorAdapter {
    private static final String TAG = MyCursorAdapter.class.getSimpleName();

    private LayoutInflater inflater;

    public MyCursorAdapter(Context context, Cursor c) {
        super(context, c);
        inflater = LayoutInflater.from(context);
    }


    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        setValue(view, cursor);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LinearLayout linearLayout = (LinearLayout) inflater.inflate(
                R.layout.list_item_student, null);
        setValue(linearLayout, cursor);
        return linearLayout;
    }

    private void setValue(View linearLayout, Cursor cursor){
        TextView mTxName = linearLayout.findViewById(R.id.txt_name);
        TextView mTxClassName = linearLayout.findViewById(R.id.txt_class);
        TextView mTxSex = linearLayout.findViewById(R.id.txt_sex);
        TextView mTxAge = linearLayout.findViewById(R.id.txt_age);
        mTxName.setText(cursor.getString(cursor.getColumnIndex("name")));
        mTxClassName.setText(cursor.getString(cursor.getColumnIndex("class_name")));
        mTxSex.setText(cursor.getShort(cursor.getColumnIndex("sex")) == 0 ? "男" : "女");
        mTxAge.setText(cursor.getShort(cursor.getColumnIndex("age")) + "岁");
    }
}